﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Linq;
//using Microsoft.ServiceModel.Web;

namespace EnterpriseServices.ConsoleHost
{
    class Program
    {  
        static void Main(string[] args)
        {
             //use the code for testing and comment out the line above
             //additionally, change the project type to console application instead of windows application

            List<ServiceHost> _hosts = new List<ServiceHost>();
            ServiceHost _host = null;

            //List<WebServiceHost2> _hosts = new List<WebServiceHost2>();
            //WebServiceHost2 _host = null;
            
            try
            {
                //var host = new WebServiceHost2(new DerivedFromCollectionServiceBase<T> (), new Uri(<--- string with ep address --->)));  

                //_host = new WebServiceHost2(new EnterpriseServices.Service.ValidationServiceREST(),
                //                                                     new Uri("http://localhost:8005/EnterpriseServices/REST/ValidationManager"));  

                _host = new ServiceHost(typeof(EnterpriseServices.Service.ValidationServiceRemoting));
                _hosts.Add(_host);

                _host = new ServiceHost(typeof(EnterpriseServices.Service.ValidationServiceBasic));
                _hosts.Add(_host);
                  
                _host = new ServiceHost(typeof(EnterpriseServices.Service.ValidationServiceJSON));
                _hosts.Add(_host);

                _host = new ServiceHost(typeof(EnterpriseServices.Service.ValidationServiceREST));
                _hosts.Add(_host);

                _host = new ServiceHost(typeof(EnterpriseServices.Service.RoleServiceRemoting));
                _hosts.Add(_host);

                _host = new ServiceHost(typeof(EnterpriseServices.Service.RoleServiceJSON));
                _hosts.Add(_host);

                _host = new ServiceHost(typeof(EnterpriseServices.Service.RoleServiceREST));
                _hosts.Add(_host);

                _host = new ServiceHost(typeof(EnterpriseServices.Service.RoleServiceBasic));
                _hosts.Add(_host);
            }
            catch (Exception ex)
            {
                //System.Diagnostics.EventLog.WriteEntry(String.Format("Failed to start WCF service:\n\n{0}\n\n{1}", ex.Message, ex.StackTrace),
                //EventLogEntryType.Error);
                throw ex;
            }

            _hosts.ForEach(host =>
            {
                if (host.State != CommunicationState.Opened)
                    host.Open();

                StringBuilder _stringBuilder = new StringBuilder();
                host.Description.Endpoints.ToList().ForEach(endpoint =>
                    _stringBuilder.Append(endpoint.Contract.Name).Append(": ").Append(endpoint.ListenUri.ToString()).Append("\n"));

                Console.WriteLine(String.Format("WCF host started successfully:\n{0}", _stringBuilder.ToString()));
            });

            Console.ReadKey();

            _hosts.ForEach(host =>
            {
                if (host.State != CommunicationState.Closed)
                    host.Close();
            });
        }
    }
}
